package com.gem.day02;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 本类用来演示JDK8.0全新日期类型
 * 
 * LocalDate,LocalDateTime,LocalTime,Instant...
 * 
 * @author easonsy
 * 2020年7月17日 下午2:46:00
 */
public class LocalDateDemo {
	public static void main(String[] args) {
		//获取当前系统时间
		LocalDate now = LocalDate.now();
		System.out.println(now);

		//获取过去或者未来的某个时间
		LocalDate date = LocalDate.of(2012, 12, 12);
		System.out.println(date);

		//获取日期信息
		System.out.println(now.getYear());
		System.out.println(now.getMonth());
		System.out.println(now.getDayOfMonth());
		System.out.println(now.getDayOfWeek());

		//设置年份
		//返回原日期对象的副本,原日期不会发生改变
		LocalDate newDate = now.withYear(2030);
		System.out.println(newDate);

		System.out.println("-----日期计算方法(副本)-----");

		//添加/减去年份
		LocalDate newDate2 = now.plusYears(10);//加年份
		LocalDate newDate3 = now.minusYears(20);//加年份
		LocalDate newDate4 = now.plus(4, ChronoUnit.MONTHS);
		System.out.println(newDate2);
		System.out.println(newDate3);
		System.out.println(newDate4);

		System.out.println("---------------------------------");

		//自定义日期格式化
		String timeStr = now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
		System.out.println(timeStr);

		System.out.println("---------------------------------");

		//获取时间差
		/*Period period = Period.between(date, now);
		System.out.println(period.getYears());
		System.out.println(period.getMonths());*/

		//时间间隔,获取两个时间之间差多少单位
		System.out.println(ChronoUnit.MONTHS.between(date, now));

		System.out.println("---------------------------------");

		//思考题:Date(传统日期)和LocalDate(新日期)之间的转换?
		LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		System.out.println(localDate);

		Date dd = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
		System.out.println(dd);
	}
}
